package Jets.ui
{
   import flash.display.DisplayObject;
   import flash.display.DisplayObjectContainer;
   import flash.display.Sprite;
   import flash.events.MouseEvent;
   import flash.events.TextEvent;
   import flash.geom.Rectangle;
   import Jets.display.JContainer;
   
   public class JRichText extends JContainer
   {
	   
	  private var contaner:Sprite;
	  private var _text:String;
	  private var _vslideDisplay:Boolean = true;
	  private var ty:int = 0;
	  
	  protected var vslide:JVScorllBar;
	  
	  
      public function JRichText(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0) 
	  {
         super(parent,cx,cy);
      }
      
      public function get vslideDisplay() : Boolean 
	  {
         return this._vslideDisplay;
      }
      public function set vslideDisplay(bool:Boolean) : void 
	  {
         this._vslideDisplay = bool;
         this.invalidate();
      }
      
      public function get container() : Sprite 
	  {
         return this.contaner;
      }
 
      override protected function addChildren() : void 
	  {
         this.contaner = new Sprite();
         this.vslide = new JVScorllBar(this,this.width,0,invalidate);
         this.contaner.addEventListener(MouseEvent.MOUSE_WHEEL,this.posvs);
         this.addChild(this.contaner);
      }
      
      protected function posvs(event:MouseEvent) : void 
	  {
         this.vslide.value = this.vslide.value + event.delta * 2;
         this.invalidate();
      }

      override public function set width(value:Number) : void 
	  {
         if(_width != value)
         {
            _width = value;
            var i:int = 0;
            while(i < this.contaner.numChildren)
            {
               (this.contaner.getChildAt(i) as JRichImage).width = _width;
               i++;
            }
            invalidateNow();
         }
      }
      
      public function appendText(text:String) : void 
	  {
         var rimage:JRichImage = null;
         if(text)
         {
			 rimage = new JRichImage();
			 rimage.width = this.width;
			 rimage.checkFaces(text);
			 rimage.y = this.height;
            this.contaner.addChild(rimage);
            invalidate();
         }
      }
      
      public function onlink(event:TextEvent) : void 
	  {
      }
      
	  /**获取最大宽度*/
      public function get maxwidth() : int 
	  {
         var jimage:JRichImage = null;
         var tw:Number = 0;
         var i:int = 0;
         while(i < this.contaner.numChildren)
         {
			 jimage = this.contaner.getChildAt(i) as JRichImage;
            if(tw < jimage.textWidth)
            {
				tw = jimage.textWidth;
            }
            i++;
         }
         return tw;
      }
      
	  /**获取当前最大高度*/
      public function get currentHeight() : int 
	  {
         var jimage:JRichImage = null;
         var th:Number = 0;
         var i:int = 0;
         while(i < this.contaner.numChildren)
         {
			 jimage = this.contaner.getChildAt(i) as JRichImage;
            if(th < jimage.height)
            {
               th = jimage.height;
            }
            i++;
         }
         return th;
      }
      
      public function get currentWidth() : int 
	  {
		  var jimage:JRichImage = null;
		  var tw:Number = 0;
		  var i:int = 0;
		  while(i < this.contaner.numChildren)
		  {
			  jimage = this.contaner.getChildAt(i) as JRichImage;
			  if(tw < jimage.textWidth)
			  {
				  tw = jimage.textWidth;
			  }
			  i++;
		  }
		  return tw;
      }
      
     
      override public function draw() : void 
	  {
		 super.draw();
		 
         var cy:Number = 0;
         var child:DisplayObject
         var htlen:Number = 0;
         
		 /**调整子控件的高度，并保存最大高度*/
         var i:int = 0;
         while(i < this.contaner.numChildren)
         {
			 child = this.contaner.getChildAt(i);
            if(child.y != cy)
            {
				child.y = cy;
            }
			cy = cy + child.height;
            i++;
         }
		 /**判断是否需要显示滚动条*/
		 cy = cy + 12;
         this.vslide.height = this.height;
         this.vslide.x = _width;
         if(cy > this.height)
         {
            this.vslide.visible = true;
			htlen = (this.vslide.height - this.vslide.arrowheight * 2) * this.height / cy;
            this.vslide.barheight = Math.max(htlen,20);
            this.ty = (100 - this.vslide.value) * (cy - this.height) / 100;
         }
         else
         {
            this.vslide.visible = false;
            this.vslide.barheight = 10;
            this.ty = 0;
         }
		 
         if(!this.vslideDisplay)
         {
            this.vslide.visible = false;
         }
		 /**强制显示指定区域内的内容*/
         this.contaner.scrollRect = new Rectangle(0,this.ty,this.width,this.height);
      }
      
	  /**清理指定0~index位置的子对象并移除 0表示全部清理移除*/
      public function clear(index:int=0) : void 
	  {
         var child:DisplayObject = null;
         if(index == 0)
         {
            while(this.contaner.numChildren)
            {
				child = this.contaner.removeChildAt(0);
               (child as JRichImage).destroy();
            }
         }
         else
         {
            var nlen:int  = Math.min(this.contaner.numChildren,index);
            var i:int = 0;
            while(i < nlen)
            {
				child = this.contaner.removeChildAt(0);
               (child as JRichImage).destroy();
               i++;
            }
         }
         invalidate();
      }
      
	  /**设置数据显示内容 value是字符串数组*/
      override public function set data(value:Object) : void 
	  {
         this.clear();
         if(value)
         {
            var i:int = 0;
            while(i < value.length)
            {
               this.appendText(value[i]);
               i++;
            }
         }
      }
      override public function get data() : Object 
	  {
         return null;
      }
   }
}
